package com.ytjj.qmyx.admin.controller.supplyChain;


import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.request.BankerContractRequest;
import com.ytjj.qmyx.admin.service.AdminService;
import com.ytjj.qmyx.admin.service.CommonService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同管理
 */
@Slf4j
@RestController
@RequestMapping("/bankerContract")
@RefreshScope
public class BankerContractController {

    @Autowired
    private SupplyApiConfig supplyApiConfig;

    @Autowired
    private AdminService adminService;


    @Resource
    private CommonService commonService;
    /**
     * 数据权限开关 true.开 false.关
     */
    @Value("${dataAuthor:false}")
    private Boolean dataAuthor;

    /**
     * 新增或修改合同
     */
    @PostMapping("/addOrUpdate")
    public String addOrUpdate(@RequestBody BankerContractRequest request){
        String userId= UserUtil.getPrincipal();
        Admin admin = adminService.getAdminByEmail(userId);
        request.setAdminName(admin.getCompellation());
        request.setRoleName(admin.getRoleName());
        request.setStatus(0);
        List<String> contractImgList = request.getContractImgList();
//        StringBuffer stringBuffer = new StringBuffer();
//        stringBuffer.append(contractImgList.get(0));
//        for (int i = 1; i < contractImgList.size(); i++) {
//            if (i < contractImgList.size() - 1){
//                stringBuffer.append(contractImgList.get(i)).append(",");
//            }else {
//                stringBuffer.append(",").append(contractImgList.get(i));
//            }
//        }
        String contractImg = StringUtils.join(contractImgList, ",");
        request.setContractImg(contractImg);
        List<String> enterTypeList = request.getEnterTypeList();
        StringBuffer stringBuf = new StringBuffer();
        if (!enterTypeList.contains("1")){
            stringBuf.append(enterTypeList.get(0));
            for (int i = 1; i < enterTypeList.size(); i++) {
                if (i < contractImgList.size() - 1) {
                    stringBuf.append(enterTypeList.get(i)).append(",");
                } else {
                    stringBuf.append(",").append(enterTypeList.get(i));
                }
            }
            request.setEnterType(stringBuf.toString());
        }else {
            request.setEnterType("1");
        }
        String s = JSONObject.toJSONString(request);
        log.info("========BankerContractRequest========={}",s);
        String result = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/bankerContract/addOrUpdate",
                s, supplyApiConfig.getHeadsMap());
        return result;
    }

    /**
     * 获取合同列表
     */
    @GetMapping("/contractList")
    public String contractList(@RequestParam(required = false) Integer bankerId,
                                     @RequestParam(required = false) String enterType,
                                     @RequestParam(required = false) Integer repayType,
                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        Map<String,String> map = new HashMap<>();
        map.put("bankerId",null != bankerId ? bankerId.toString() : "");
        map.put("enterType",enterType);
        map.put("repayType", null != repayType ? repayType.toString() : "");
        map.put("pageNum",pageNum.toString());
        map.put("pageSize",pageSize.toString());
        if (dataAuthor){
            String adminName = commonService.getAdminName();
            if (StringUtils.isNotBlank(adminName)){
                map.put("adminName",adminName);
            }
        }
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/bankerContract/contractList", map, supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 获取全部的供应商
     */
    @GetMapping("/getAllBankerList")
    public String getAllBankerList(@RequestParam(required = false) String bankerName){
        Map<String,String> map = new HashMap<>();
        map.put("bankerName",bankerName);
        if (dataAuthor){
            String adminName = commonService.getAdminName();
            if (StringUtils.isNotBlank(adminName)){
                map.put("adminName",adminName);
            }
        }
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/bankerContract/getAllBankerList", map, supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 合同详情
     */
    @GetMapping("/contractDetail")
    public String contractDetail(@RequestParam Integer id){
        Map<String,String> map = new HashMap<>();
        map.put("id",id.toString());
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/bankerContract/contractDetail", map, supplyApiConfig.getHeadsMap());
        return s;
    }
}
